package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.BootstrapEditText;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.adapter.TaskAdapter;
import palmtop.com.palmtop.adapter.WxClueResAdapter;
import palmtop.com.palmtop.event.CluelistSyncEvent;
import palmtop.com.palmtop.model.Clue;
import palmtop.com.palmtop.model.ClueWx;
import palmtop.com.palmtop.model.Unit;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class ClueWxdetailActivity extends BaseActivity {
    MyApplication app;
    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.reportman)
    AwesomeTextView reportman;
    @BindView(R.id.position)
    AwesomeTextView position;
    @BindView(R.id.lookmapBtn)
    Button lookmapBtn;
    @BindView(R.id.addtime)
    AwesomeTextView addtime;
    @BindView(R.id.cluecontent)
    AwesomeTextView cluecontent;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.huifuList)
    ListView huifuList;
    @BindView(R.id.rescontent)
    BootstrapEditText rescontent;
    @BindView(R.id.save)
    Button save;

    Clue clue;
    WxClueResAdapter wxClueResAdapter;
    String userid;
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clue_wxdetail, Config.toptitle, 1);
        ButterKnife.bind(this);
        app = (MyApplication) getApplication();
        app.addActivity(ClueWxdetailActivity.this);
        initView();

    }

    private void initView() {
        clue = (Clue) getIntent().getSerializableExtra("clue");
        AppPreferences appPreferences = new AppPreferences(ClueWxdetailActivity.this);
        userid = appPreferences.getString("userid", "");
        name = appPreferences.getString("name", "");
        String role_id = appPreferences.getString("role_id", "");
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            FuncUtil.updateReadClue(String.valueOf(clue.getId()));
        }
        mtitle.setText("线索详情");

        if(clue!=null){
            reportman.setText(clue.getReportman());
            position.setText(clue.getPosition());
            addtime.setText(clue.getAddtime());
            cluecontent.setText(clue.getCluecontent());
            if (clue.getPhoto1() != null && !clue.getPhoto1().equals("")) {
                x.image().bind(zphoto1, Config.IMG_URL + clue.getPhoto1());
                FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto1(), zphoto1, ClueWxdetailActivity.this);
            }
            if (clue.getPhoto2() != null && !clue.getPhoto2().equals("")) {
                x.image().bind(zphoto2, Config.IMG_URL + clue.getPhoto2());
                FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto2(), zphoto2, ClueWxdetailActivity.this);
            }
            if (clue.getPhoto3() != null && !clue.getPhoto3().equals("")) {
                x.image().bind(zphoto3, Config.IMG_URL + clue.getPhoto3());
                FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto3(), zphoto3, ClueWxdetailActivity.this);
            }
            if (clue.getPhoto4() != null && !clue.getPhoto4().equals("")) {
                x.image().bind(zphoto4, Config.IMG_URL + clue.getPhoto4());
                FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto4(), zphoto4, ClueWxdetailActivity.this);
            }
            getRescontent(clue.getId());
        }
    }

    private void getRescontent(int id) {
        RequestParams params = new RequestParams(Config.SERVER_URL + "wxclue/getByClueid");
        params.addBodyParameter("clueid",String.valueOf(id));
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<ClueWx>>() {}.getType();
                ArrayList<ClueWx> list = gson.fromJson(result, listType);
                wxClueResAdapter = new WxClueResAdapter(list, ClueWxdetailActivity.this);
                huifuList.setAdapter(wxClueResAdapter);
                setListViewHeightBasedOnChildren(huifuList);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        //获得adapter
        WxClueResAdapter adapter = (WxClueResAdapter) listView.getAdapter();
        if (adapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0);
            //计算总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        //计算分割线高度
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        //给listview设置高度
        listView.setLayoutParams(params);
    }

    @OnClick(R.id.lookmapBtn)
    public void onLookmapBtnClicked() {
        Intent i = new Intent();
        if (!TextUtils.isEmpty(clue.getLatitude())) {
            i.putExtra("latitude", clue.getLatitude());
        }
        if (!TextUtils.isEmpty(clue.getLongitude())) {
            i.putExtra("longitude", clue.getLongitude());
        }
        i.setClass(this, LookmapActivity.class);
        startActivityForResult(i, 1);
    }

    @OnClick(R.id.save)
    public void onSaveClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "wxclue/save");
        params.addBodyParameter("clueid", String.valueOf(clue.getId()));
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addtime = df.format(day);
        params.addBodyParameter("addtime", addtime);
        params.addBodyParameter("reportid", userid);
        params.addBodyParameter("reportname", name);
        params.addBodyParameter("rescontent", String.valueOf(rescontent.getText()));

        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(ClueWxdetailActivity.this, "提交成功");
                    EventBus.getDefault().post(new CluelistSyncEvent());
                    finish();
                } else {
                    ToastUtil.show(ClueWxdetailActivity.this, "提交失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }
}
